<?php
/*
 * @Description: 华为云短信类
 * @Author: QianLong
 * @Date: 2019-09-27 16:34:02
 * @LastEditors    : QianLong
 * @LastEditTime   : 2024-01-16 17:05:47
 */

namespace App\Service\Sms;

use Yurun\Util\HttpRequest;

class HuaSms
{
    /**
     * 接口请求
     */
    public function sendRequest($phone, $templateCode, $templateParam, $huawei_app_key, $huawei_app_secret, $huawei_app_url, $huawei_sign_no, $huawei_sign,$extend='')
    {
        if (is_array($templateParam)) {
            $templateParam = json_encode($templateParam);
        }
        $smsParam['from'] = $huawei_sign_no;
        $smsParam['to'] = $phone;
        $smsParam['templateId'] = $templateCode;
        $smsParam['templateParas'] = $templateParam;
        $smsParam['extend'] = $extend;
        $smsParam['signature'] = $huawei_sign;
        $huawei_app_url = rtrim($huawei_app_url,'/');
        $nonce = createULId();
        $created = date('Y-m-d\TH:i:s\Z');
        $password = base64_encode(hash("sha256", $nonce. $created. $huawei_app_secret));
        $http = new HttpRequest;
        $response = $http->header('Authorization', 'WSSE realm="SDP",profile="UsernameToken",type="Appkey"')->header('X-WSSE', 'UsernameToken Username="'. $huawei_app_key.'",PasswordDigest="'. $password.'",Nonce="'. $nonce.'",Created="'. $created.'"')->post($huawei_app_url. '/sms/batchSendSms/v1', $smsParam);
        $result = json_decode($response->body(), true);
        $postData['send_result'] = ($result['code'] == '000000') ? true : false;
        $postData['send_error'] = ($result['code'] != '000000') ? $result['description'] : '';
        $postData['send_result_data'] = json_encode($result, JSON_UNESCAPED_UNICODE);
        $postData['smsParam'] = json_encode($smsParam, JSON_UNESCAPED_UNICODE);
        return $postData;
    }
}
